




//
//  NMessageViewController.m
//  nncloud
//
//  Created by macfai on 2018/11/19.
//  Copyright © 2018 macfai. All rights reserved.
//

#import "NMessageViewController.h"
#import "NMsgCell.h"
#import "NRenzhengAndCuibaoVC.h"
#import "NXiTongNotiAndWentiFankuiVC.h"
#import "NMsgModel.h"
#import "PPBadgeView.h"
@interface NMessageViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong)UITableView *tableView;

@property (nonatomic,strong)NSMutableArray *dataArray;

@property (nonatomic,strong)NSArray *titles;

@property (nonatomic,strong)NSArray *picArray;

@property (nonatomic,strong)NSArray *detailsArray;


@end

@implementation NMessageViewController

#define cellIdentifier @"msg"
#define SectionHeaderHeight 15.f

-(UITableView *)tableView{
    if (!_tableView) {
        WeakStament(ws);
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0 , 0, ScreenWidth , ScreenHeight - 64) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.rowHeight = 85.0f;
        _tableView.backgroundColor = kBackgroundColor;
        [_tableView registerNib:[UINib nibWithNibName:@"NMsgCell" bundle:nil] forCellReuseIdentifier:cellIdentifier];
//        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
////            [ws loadData];
//        }];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.tableFooterView = [UIView new];
        
    }
    return _tableView;
}


///数组删除测试
-(void)test{
    
    NSMutableArray *array = [NSMutableArray arrayWithArray:@[@"1",@"2",@"3",@"4",@"5"]];
    
    [array enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        if ([obj isEqualToString:@"3"]){
            
            [array removeObject:obj];
            
        }
        
        if (idx==3){
            
            [array removeObject:obj];
            
        }
        
        
    }];
    
    NSLog(@"数组删除测试：%@",array);
    
}


- (void)viewDidLoad {
    
    [super viewDidLoad];
    self.view.backgroundColor = kWhiteColor;
    self.dataArray = [NSMutableArray array];
    ///0个人  1企业  2政府
    
    if ([_userType isEqualToString:@"1"]) {

        self.picArray = @[@"问题反馈",@"催报通",@"系统通知",@"认证通",];
        self.titles = @[@"问题反馈",@"催报通知",@"系统通知",@"认证通知",];

    }else if ([_userType isEqualToString:@"0"]){
        //个人
        self.picArray = @[@"问题反馈",@"系统通知",@"认证通"];
        self.titles = @[@"问题反馈",@"系统通知",@"认证通知",];
    }else{
        
        self.picArray = @[@"问题反馈",@"系统通知",@"",@""];
        self.titles = @[@"问题反馈",@"系统通知",@"",@""];
        
    }
    [self.navigationItem setItemWithTitle:@"消息通知" textColor:kNavigationTitleColor fontSize:18 itemType:center];
    [self.view addSubview:self.tableView];
    [self loadData];
    
//    [self test];
    // Do any additional setup after loading the view from its nib.
}

- (void)loadData{
    WeakStament(ws);
    NSDictionary *dic = @{@"messageUserId":[NUserModel shareInstance].userInfoId?:@""};
    [[NNRCNetRequest manager]getWithUrl:kMsgNotiHomeUrl params:dic success:^(id result) {
        YYLog(@"%@",result);
        if ([[result objectForKey:@"code"] integerValue] == 0) {
            NSArray *arr = result[@"page"];
            YYLog(@"%@",arr);
            for (NSDictionary *dict in arr) {
                NMsgModel *model = [NMsgModel mj_objectWithKeyValues:[dict deleteNull]];
                
                [ws.dataArray addObject:model];
                
                if ([ws.userType isEqualToString:@"2"]) {
                    //政府端  2个
                    if (ws.dataArray.count>3) {
                        //逆序删除可变数组中的元素 不会导致崩溃
                        [ws.dataArray enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                            if (idx==2) {
                                 [ws.dataArray removeObject:obj];
                            }
                            if (idx==3){
                                [ws.dataArray removeObject:obj];
                            }
                        }];
                    }
                }
                else if ([ws.userType isEqualToString:@"1"]){
                    //企业端
                    //企业端不做处理 直接保持最初的数据就可以
                }
                else{
                    //个人端3个
                    if (ws.dataArray.count>3) {
                        //逆序删除可变数组中的元素 不会导致崩溃
                        [ws.dataArray enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                            if (idx==3){
                                [ws.dataArray removeObject:obj];
                            }
                        }];
                    }
                
                }
                
            }
            [ws.tableView reloadData];
        }else{
            [AFToast showText:result[@"msg"]];
        }
    } failure:^(id failure) {
        YYLog(@"%@",failure);
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
//    注意这里用到的个数的数组一定要是dataArray  如果是另外一个就不行
       return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NMsgCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
    NMsgModel *model = self.dataArray[indexPath.row];
    cell.nameLabel.text = self.titles[indexPath.row];
    cell.img.image = WDImgName(self.picArray[indexPath.row]);
    ///添加红点
    [cell.img pp_addBadgeWithNumber:[model.unReadCount integerValue]];
    [cell.img pp_moveBadgeWithX:0 Y:3];
    cell.detailLabel.text = model.messageContent;
//    if ([_userType isEqualToString:@"0"]) {
//        if (indexPath.row==3) {
//            cell.backgroundColor = kClearColor;
//            cell.
//        }
//    }
    return cell;
}

#pragma mark - delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    switch (indexPath.row) {
        case 0:
        {
            //1
            NXiTongNotiAndWentiFankuiVC *noti = [NXiTongNotiAndWentiFankuiVC new];
            noti.atitle = @"问题反馈";
            noti.msgType = @"1";
            [self.navigationController pushViewController:noti animated:YES];
        }
            break;
        case 1:
        {
            //催报  2
            NRenzhengAndCuibaoVC *cuibao = [NRenzhengAndCuibaoVC new];
            cuibao.atitle = @"催报通知";
            cuibao.msgType = @"2";
            [self.navigationController pushViewController:cuibao  animated:YES];
        }
            break;
        case 2:
        {
            //3
            NXiTongNotiAndWentiFankuiVC *noti = [NXiTongNotiAndWentiFankuiVC new];
            noti.atitle = @"系统通知";
            noti.msgType = @"3";
            [self.navigationController pushViewController:noti animated:YES];
        }
            break;
        case 3:
        {
            //认证  4
            NRenzhengAndCuibaoVC *cuibao = [NRenzhengAndCuibaoVC new];
             cuibao.atitle = @"认证通知";
            cuibao.msgType = @"4";
            [self.navigationController pushViewController:cuibao  animated:YES];
        }
            break;
            
        default:
            break;
    }
    
}


#pragma mark - section head
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return SectionHeaderHeight;//如果设置section的header高度，不设置footer高度，footer默认等于header高度
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, SectionHeaderHeight)];
    view.backgroundColor = kBackgroundColor;
    return view;
}

#pragma mark - section foot
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return nil;
}

@end
